package com.web.kdl.daoV2.flyWheel;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.web.kdl.entity.maDataReport.DataReport;
import com.web.kdl.pojo.Vo.RT.RtDataVo;
import com.web.kdl.pojo.Vo.flywheel.FlywheelVo;
import com.web.kdl.pojo.dto.FlywheelMainDto;
import com.web.kdl.entity.flywheel.FlyWheel;
import com.web.kdl.pojo.Vo.*;
import com.web.kdl.pojo.Vo.flywheel.FlywheelAlertInfoVo;
import com.web.kdl.pojo.Vo.flywheel.FlyWheelEleCountVo;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author： shulibin
 * @Date： 2025/7/24 8:49
 * @Describe：
 */
@Mapper
public interface FlyWheelDao extends BaseMapper<FlyWheel> {
//    @Select("SELECT * FROM #{tableNum} WHERE id = #{id}")
//    FlyWheel selectById(@Param("id") int id, @Param("tableNum") int tableNum);

    @Select({
            "SELECT ",
            "    DATE(time) AS date, ",
            "    AVG(data_value) AS average_usage ",
            "FROM `${tableName}`",
            "WHERE ",
            "    device_name = '系统SOC' ",
            "    AND time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) ",
            "    AND time < CURDATE() ",
            "GROUP BY ",
            "    DATE(time) ",
            "ORDER BY ",
            "    date"
    })
    List<Map<String, Object>> selectAverageSocUsage(@Param("tableName") String tableName);

    @Select({
            "SELECT ",
            "    DATE(time) AS date, ",
            "    AVG(CASE WHEN device_name = '系统可放电功率' THEN data_value ELSE NULL END) AS discharge_avg, ",
            "    AVG(CASE WHEN device_name = '系统可充电功率' THEN data_value ELSE NULL END) AS charge_avg ",
            "FROM `${tableName}`",
            "WHERE ",
            "    device_name IN ('系统可放电功率', '系统可充电功率') ",
            "    AND time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) ",
            "    AND time < CURDATE() ",
            "GROUP BY ",
            "    DATE(time) ",
            "ORDER BY ",
            "    date"
    })
    List<DailyPowerAvgVo> selectDailyPowerAvg(@Param("tableName") String tableName);

    @Select({
            "WITH monthly_data AS (",
            "    SELECT ",
            "        *,",
            "        DATE_FORMAT(time, '%Y-%m') AS month,",
            "        ROW_NUMBER() OVER (",
            "            PARTITION BY device_name, DATE_FORMAT(time, '%Y-%m') ",
            "            ORDER BY time DESC",
            "        ) AS rn",
            "    FROM `${tableName}`",  // 这里必须换行
            "    WHERE device_name IN ('充电计数', '放电计数')",
            "),",
            "last_day_data AS (",
            "    SELECT *",
            "    FROM monthly_data",
            "    WHERE rn = 1",
            "),",
            "months AS (",
            "    SELECT DATE_FORMAT(DATE_SUB(CURRENT_DATE, INTERVAL n MONTH), '%Y-%m') AS month",
            "    FROM (",
            "        SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3",
            "        UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7",
            "        UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11",
            "    ) months",
            ")",
            "SELECT ",
            "    m.month,",
            "    MAX(CASE WHEN d.device_name = '充电计数' THEN d.data_value ELSE NULL END) AS chargeCount,",
            "    MAX(CASE WHEN d.device_name = '放电计数' THEN d.data_value ELSE NULL END) AS dischargeCount ",
            "FROM months m ",
            "LEFT JOIN last_day_data d ON DATE_FORMAT(d.time, '%Y-%m') <= m.month ",
            "GROUP BY m.month ",
            "ORDER BY m.month ASC"
    })
    @Results({
            @Result(property = "month", column = "month"),
            @Result(property = "chargeCount", column = "chargeCount"),
            @Result(property = "dischargeCount", column = "dischargeCount")
    })
    List<FlyWheelEleCountVo> selectLastDayChargeDischargeStats(@Param("tableName") String tableName);


    @Select("SELECT * " +
            "FROM `${tableName}`" +
            "WHERE " +
            "    time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)" +
            "    AND time < CURDATE()")
    List<FlywheelAlertInfoVo> selectFlywheelUnitInfo(@Param("tableName") String tableName);

//  获取当前时间最新数据
@Select("SELECT w.status_value, w.device_name, w.status_name, w.time " +
        "FROM `${tableName}` w " +
        "WHERE w.status_name IN ('通信模式', '驱动1组运行状态', '驱动2组运行状态', '电机工作状态', '空调运行状态') " +
        "AND w.time IN (" +
        "  SELECT MAX(time) " +
        "  FROM `${tableName}` " +
        "  WHERE status_name IN ('通信模式', '驱动1组运行状态', '驱动2组运行状态', '电机工作状态', '空调运行状态') " +
        "  GROUP BY status_name " +
        ") " +
        "ORDER BY w.time DESC")
    @Results({
            @Result(column = "status_value", property = "dataValue"),
            @Result(column = "status_name", property = "deviceName"),
            @Result(column = "time", property = "time")
    })
    List<FlywheelMainDto> selectFlywheelMainInfo1(@Param("tableName") String tableName);

    @Select("SELECT id, sum_site, sub_site, time, data_name, data_value, unit, device_name " +
            "FROM `${tableName}` w " +
            "WHERE (w.device_name, w.time) IN (" +
            "  SELECT device_name, MAX(time) " +
            "FROM `${tableName}`" +
            "  WHERE device_name IN ('系统SOC', '系统容量', '转子温度位移推算值', '转速', '真空度1'," +
            "                        '母线电压','相电流有效值','电机输出功率','柜内环境温度'," +
            "                        '驱动1组母线电压1','驱动2组母线电压1','上端轴向位移1','上端轴向位移2'," +
            "                        '下端轴向位移1','下端轴向位移2') " +
            "  GROUP BY device_name " +
            ") " +
            "ORDER BY time DESC")
    @Results({
            @Result(column = "data_value", property = "dataValue"),
            @Result(column = "device_name", property = "deviceName"),
            @Result(column = "unit", property = "unit"),
            @Result(column = "time", property = "time")
    })
    List<FlywheelMainDto> selectFlywheelMainInfo2(@Param("tableName") String tableName);


    
    List<EnergyDataVo> selectFlywheelEleCount(@Param("tableName") String tableName);

    @Select({
            "SELECT device_name, CONCAT(data_value,unit) as value " +
                    "FROM `${tableName}` " +
                    "WHERE time >= DATE_SUB(NOW(), INTERVAL 15 YEAR)"
    })
    @Results({
            @Result(column = "device_name", property = "property"),
            @Result(column = "value", property = "propertyValue")
    })
    ArrayList<RtDataVo> selectDataProperty(@Param("tableName") String tableName);

    @Select({
            "SELECT status_name, status_value " +
            "FROM `${tableName}` " +
            "WHERE time >= DATE_SUB(NOW(), INTERVAL 15 YEAR)"
    })
    @Results({
            @Result(column = "status_name", property = "property"),
            @Result(column = "status_value", property = "propertyValue")
    })
    ArrayList<RtDataVo> selectStatusProperty(@Param("tableName") String TableName);

    /**
     * 查询最新一条数据
     */
    @Select("SELECT id, device_name as deviceName, data_value as dataValue, unit, time " +
            "FROM ${tableName} " +
            "WHERE time = (SELECT MAX(time) FROM ${tableName}) " +
            "ORDER BY id DESC")
    List<DataReport> getLastValue(@Param("tableName") String tableName);
}